/*
 * @Author: tanjunyi@doctorwork.com
 * @Date: 2020-06-21 17:02:14
 * @LastEditors: tanjunyi@doctorwork.com
 * @LastEditTime: 2020-06-21 19:10:11
 * @Description:
 */

let id = 0;
class Dep {
	constructor() {
		this.id = id++;
		this.subs = [];
	}

	depend() {
		// 1. 让dep记住watcher
		// 2. 让watcher记住dep
		Dep.target.addDep(this); //让watcher存储dep
	}

	addSub(watcher) {
		this.subs.push(watcher);
	}

	notify() {
		this.subs.forEach(watcher => watcher.update())
	}
}

Dep.target = null; //临时存储用
const stack = [];
//  dep 和watch 多对多
//  每个属性都有一个dep属性，dep存放着 一个或多个watcher，一个watcher可能被多个dep依赖
export function pushTarget(watcher) {
	Dep.target = watcher;
	stack.push(watcher);
}

export function popTarget() {
	Dep.target = stack[stack.length - 1]
}

export default Dep;